package com.ls.x.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ls.x.activity.beans.ContextBean;
import com.ls.x.tool.DatabaseHelper;

public class ContextDao implements IContextDao {

	private DatabaseHelper databaseHelper;

	public ContextDao(DatabaseHelper databaseHelper) {
		super();
		this.databaseHelper = databaseHelper;
	}

	@Override
	public ContextBean getContextById(int id) {

		SQLiteDatabase sd = databaseHelper.getReadableDatabase();
		Cursor cursor = sd.rawQuery("select * from context where id=" + id,
				null);

		ContextBean contextBean = new ContextBean();
		contextBean
				.setUserId(cursor.getString(cursor.getColumnIndex("user_id")));
		contextBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
		contextBean.setSummary(cursor.getString(cursor
				.getColumnIndex("summary")));
		contextBean.setLevel(cursor.getString(cursor.getColumnIndex("level")));
		contextBean.setStartDate(new Date(cursor.getLong(cursor
				.getColumnIndex("start_date"))));
		contextBean.setEndDate(new Date(cursor.getLong(cursor
				.getColumnIndex("end_date"))));
		contextBean
				.setRepeat(cursor.getString(cursor.getColumnIndex("repeat")));
		contextBean.setClassName(cursor.getString(cursor
				.getColumnIndex("class")));
		contextBean.setPosition(cursor.getString(cursor
				.getColumnIndex("position")));
		contextBean.setMode(cursor.getInt(cursor.getColumnIndex("mode")));
		contextBean.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
		contextBean
				.setRemark(cursor.getString(cursor.getColumnIndex("remark")));
		
		cursor.close();
		sd.close();
		
		return contextBean;
	}

	@Override
	public List<ContextBean> getAllContextBeans() {

		List<ContextBean> contextBeanList = new ArrayList<ContextBean>();

		SQLiteDatabase sd = databaseHelper.getReadableDatabase();
		Cursor cursor = sd.rawQuery("select * from context",
				null);

		while (cursor.moveToNext()) {
			ContextBean contextBean = new ContextBean();
			contextBean.setUserId(cursor.getString(cursor
					.getColumnIndex("user_id")));
			contextBean.setTitle(cursor.getString(cursor
					.getColumnIndex("title")));
			contextBean.setSummary(cursor.getString(cursor
					.getColumnIndex("summary")));
			contextBean.setLevel(cursor.getString(cursor
					.getColumnIndex("level")));
			contextBean.setStartDate(new Date(cursor.getLong(cursor
					.getColumnIndex("start_date"))));
			contextBean.setEndDate(new Date(cursor.getLong(cursor
					.getColumnIndex("end_date"))));
			contextBean.setRepeat(cursor.getString(cursor
					.getColumnIndex("repeat")));
			contextBean.setClassName(cursor.getString(cursor
					.getColumnIndex("class")));
			contextBean.setPosition(cursor.getString(cursor
					.getColumnIndex("position")));
			contextBean.setMode(cursor.getInt(cursor.getColumnIndex("mode")));
			contextBean
					.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
			contextBean.setRemark(cursor.getString(cursor
					.getColumnIndex("remark")));

			contextBeanList.add(contextBean);
		}
		
		cursor.close();
		sd.close();
		return contextBeanList;

	}

	@Override
	public List<ContextBean> getContextBeansWhereModeIsNotNull() {
		List<ContextBean> contextBeanList = new ArrayList<ContextBean>();

		SQLiteDatabase sd = databaseHelper.getReadableDatabase();
		Cursor cursor = sd.rawQuery("select * from context where mode<>''",
				null);

		while (cursor.moveToNext()) {
			ContextBean contextBean = new ContextBean();
			contextBean.setUserId(cursor.getString(cursor
					.getColumnIndex("user_id")));
			contextBean.setTitle(cursor.getString(cursor
					.getColumnIndex("title")));
			contextBean.setSummary(cursor.getString(cursor
					.getColumnIndex("summary")));
			contextBean.setLevel(cursor.getString(cursor
					.getColumnIndex("level")));
			contextBean.setStartDate(new Date(cursor.getLong(cursor
					.getColumnIndex("start_date"))));
			contextBean.setEndDate(new Date(cursor.getLong(cursor
					.getColumnIndex("end_date"))));
			contextBean.setRepeat(cursor.getString(cursor
					.getColumnIndex("repeat")));
			contextBean.setClassName(cursor.getString(cursor
					.getColumnIndex("class")));
			contextBean.setPosition(cursor.getString(cursor
					.getColumnIndex("position")));
			contextBean.setMode(cursor.getInt(cursor.getColumnIndex("mode")));
			contextBean
					.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
			contextBean.setRemark(cursor.getString(cursor
					.getColumnIndex("remark")));

			contextBeanList.add(contextBean);
		}
		return contextBeanList;
	}
}
